package com.ppt.android.base.utils;

import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.StyleSpan;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;

/**
 * @author ZouJiaCai
 * @since 2023/7/15 15:11
 */
public class ExpandTextUtils {
    private String message;
    private AppCompatTextView textView;

    public ExpandTextUtils(String message, AppCompatTextView textView) {
        this.message = message;
        this.textView = textView;
    }

    public void onMessage() {
        if (message == null || message.isEmpty()){
//            this.message = "默认字符串";
            return;
        }
        if (this.message.length() > 30) {
            setMessage(2);
        } else {
            setMessage(1);
        }
    }

    public void setMessage(int label) {
        if (message != null && !message.isEmpty()) {
            String str;//显示的字符串
            str = message;
            SpannableString spannableString = null;
            if (str.length() <= 30)//字符串长度不够
                label = 1;
            switch (label) {
                case 2:
                    //展开
                    str = str.substring(0, 30);
                    str += "...展开";
                    spannableString = new SpannableString(str);
                    //可点击
                    ClickableSpan clickableSpan1 = new ClickableSpan() {
                        @Override
                        public void onClick(@NonNull View widget) {
                            setMessage(3);
                        }

                        @Override
                        public void updateDrawState(@NonNull TextPaint ds) {
                            super.updateDrawState(ds);
                            ds.setColor(Color.parseColor("#FFFFFF"));
                        }
                    };
                    spannableString.setSpan(clickableSpan1, str.length() - 2, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    break;
                case 3:
                    //收起
                    str += "收起";
                    spannableString = new SpannableString(str);
                    //可点击
                    ClickableSpan clickableSpan2 = new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                            setMessage(2);
                        }

                        @Override
                        public void updateDrawState(@NonNull TextPaint ds) {
                            super.updateDrawState(ds);
                            ds.setColor(Color.parseColor("#FFFFFF"));
                        }
                    };
                    spannableString.setSpan(clickableSpan2, str.length() - 2, str.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    //收起
                    break;
                default:
                    //直接展示
                    spannableString = new SpannableString(str);
                    break;
            }
//            StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
//            spannableString.setSpan(styleSpan, 0, 3, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            this.textView.setText(spannableString);
            this.textView.setMovementMethod(LinkMovementMethod.getInstance());
        }
    }
}
